from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action

from rest_framework.status import HTTP_201_CREATED,HTTP_404_NOT_FOUND,HTTP_204_NO_CONTENT
# Create your views here.
from apps.study.models import Student, Classes
from apps.study.serializers import StudentSerializer,ClassSerializer

"""
class StudentViewSet(viewsets.ViewSet):
    def list(self,request): # 查看功能
        # 获取queryset
        queryset = Student.objects.all()
        # 序列化,实例化序列化器，将查询集转为了json格式数据
        serialzer = StudentSerializer(queryset,many=True)
        return Response(serialzer.data) # data是JSON里面的数据

    def create(self,request):
        '''
        1、获取前端传入数据
        2、校验
        3、保存数据
        4、返回响应
        :param request:
        :return:
        '''
        data=request.data
        # 反系列化
        serilaizer = StudentSerializer(data=data)
        serilaizer.is_valid(raise_exception=True)
        serilaizer.save()
        return Response(serilaizer.data,status=HTTP_201_CREATED)

    def retrieve(self,request,pk):
        '''
        1、依据pk查看指定对象，
            如果有，系列化
            如果没有，返回404,找不到学生信息
        2、返回响应
        :param request:
        :param pk:
        :return:
        '''
        try:
            student=Student.objects.get(id=pk)
        except Student.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)

        serializer=StudentSerializer(instance=student)
        return Response(serializer.data)

    def update(self,request,pk):
        '''
         1、依据pk查看指定对象，
              如果有，系列化
              如果没有，返回404,找不到学生信息
        2、获取前端传入的数据
        3、校验
        4、保存数据
        5、返回响应

        :param request:
        :param pk:
        :return:
        '''
        try:
            student=Student.objects.get(id=pk)
        except Student.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)

        serializer=StudentSerializer(instance=student,data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def destroy(self,request,pk):
        try:
            student=Student.objects.get(id=pk)
        except Student.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)

        student.delete() # 数据会直接删除
        return Response(status=HTTP_204_NO_CONTENT)
"""
class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.filter(is_delete=False)
    serializer_class = StudentSerializer

class ClassesViewSet(viewsets.ModelViewSet):
    """
    list:
    查看全部班级信息
    create:
    新增班级信息
    retrieve:
    查看某个班级信息
    update:
    修改某个班级信息
    destroy:
    删除某个班级信息
    last:
    查看最新班级信息
    """

    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassSerializer

    @action(methods=['get'],detail=False) #  detail如果是True要带pk
    def last(self,request):
        last_class= Classes.objects.filter(is_delete=False).last()
        serializer= self.get_serializer(last_class) # 序列化对象
        return  Response(serializer.data)